package com.phospherus.common.service;

import com.phospherus.common.repository.BaseRepository;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/11/8
 */
public interface IBaseService<T, ID extends Serializable, REPOSITORY extends BaseRepository<T, ID>> {
    /**
     * Saves a given com.sqi.datastool.uc.server.entity. Use the returned instance for further operations as the save operation might have changed the
     * com.sqi.datastool.uc.server.entity instance completely.
     *
     * @param entity
     * @return the saved com.sqi.datastool.uc.server.entity
     */
    <S extends T> S save(S entity);

    /**
     * Saves all given entities.
     *
     * @param entities
     * @return the saved entities
     * @throws IllegalArgumentException in case the given com.sqi.datastool.uc.server.entity is {@literal null}.
     */
    <S extends T> Iterable<S> save(Iterable<S> entities);

    /**
     * Retrieves an com.sqi.datastool.uc.server.entity by its id.
     *
     * @param id must not be {@literal null}.
     * @return the com.sqi.datastool.uc.server.entity with the given id or {@literal null} if none found
     * @throws IllegalArgumentException if {@code id} is {@literal null}
     */
    T findOne(ID id);

    /**
     * Returns whether an com.sqi.datastool.uc.server.entity with the given id exists.
     *
     * @param id must not be {@literal null}.
     * @return true if an com.sqi.datastool.uc.server.entity with the given id exists, {@literal false} otherwise
     * @throws IllegalArgumentException if {@code id} is {@literal null}
     */
    boolean exists(ID id);

    /**
     * Returns all instances of the type.
     *
     * @return all entities
     */
    List<T> findAll();

    /**
     * Returns all instances of the type with the given IDs.
     *
     * @param ids
     * @return
     */
    List<T> findAll(Iterable<ID> ids);

    /**
     * 分页查询所有
     *
     * @param currPage 当前页
     * @param pageSize 每页条数
     * @return
     */
    Page<T> findAll(int currPage, int pageSize);

    /**
     * 按条件查询
     *
     * @param param 查询条件
     * @return
     */
    List<T> findAll(Map<String, String> param);

    /**
     * 按条件查询,分页
     *
     * @param param    查询条件
     * @param currPage 当前页，从0开始
     * @param pageSize 每页条数
     * @return
     */
    Page<T> findAll(Map<String, String> param, int currPage, int pageSize);


    /**
     * Returns the number of entities available.
     *
     * @return the number of entities
     */
    long count();

    /**
     * Deletes the com.sqi.datastool.uc.server.entity with the given id.
     *
     * @param id must not be {@literal null}.
     * @throws IllegalArgumentException in case the given {@code id} is {@literal null}
     */
    void delete(ID id);
}
